package cn.lds.j368.dsbank.service.impl;

import cn.lds.j368.dsbank.constant.DsBankConstants;
import cn.lds.j368.dsbank.domain.Customer;
import cn.lds.j368.dsbank.dto.RespDTO;
import cn.lds.j368.dsbank.exception.ServiceException;
import cn.lds.j368.dsbank.mapper.CustomerMapper;
import cn.lds.j368.dsbank.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public RespDTO<List<Customer>> selectCustomerAllByCreateEmpId(Integer empId) throws ServiceException {

        List<Customer> customers = customerMapper.selectCustomerAllByCreateEmpId(empId);

        // 如果没有查询到数据，我可以考虑采用这种方式抛出异常信息
        if (CollectionUtils.isEmpty(customers)){
            throw new ServiceException(DsBankConstants.SysMsg.QUERY_FAIL);
        }

        return RespDTO.buildSuccess(customers);
    }

    @Override
    public RespDTO<Customer> selectCustomerByCustomerId(Long customerId) throws ServiceException {

        Customer customer = customerMapper.selectCustomerByCustomerId(customerId);

        if (Objects.isNull(customer)){
            throw new ServiceException(DsBankConstants.SysMsg.QUERY_FAIL);
        }


        return RespDTO.buildSuccess(customer);
    }

    @Override
    public RespDTO<Customer> selectCustomerByCertificateNo(String certificateNo) {

        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>(new Customer()).eq("CERTIFICATE_NO",certificateNo);

        Customer customer = customerMapper.selectOne(customerQueryWrapper);

        if (Objects.isNull(customer)){
            return RespDTO.buildError("该用户不存在");
        }

        return RespDTO.buildSuccess(customer);
    }

    @Override
    public RespDTO addCustomer(Customer customer) throws ServiceException {

        int insert = customerMapper.insert(customer);

        if (insert <=0 ){
            throw new ServiceException(DsBankConstants.SysMsg.ADD_DATA_FAIL);
        }

        return RespDTO.buildSuccess();
    }

    @Override
    public RespDTO updateCustomer(Customer customer) throws ServiceException {

        UpdateWrapper<Customer> customerUpdateWrapper = new UpdateWrapper<>(new Customer());

        int i = customerMapper.updateById(customer);

        if (i < 0){
            throw new ServiceException(DsBankConstants.SysMsg.MODIFY_DATA_FAIL);
        }
        return RespDTO.buildSuccess();
    }

    @Override
    public RespDTO delCustomerByCustomerId(Integer customerId) throws ServiceException {

        int i = customerMapper.deleteById(customerId);

        if (i <=0 ){
            throw new ServiceException(DsBankConstants.SysMsg.DEL_DATA_FAIL);
        }

        return RespDTO.buildSuccess();
    }
}
